package com.xcity.game.peakroad.combat;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.unit.CombatableBase;
import com.xcity.game.npc.NpcCombatable;
import com.xcity.game.npc.NpcTeamTemplate;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.peakroad.PeakRoadUtils;
import com.xcity.game.peakroad.model.PeakRoadInstance;
import com.xcity.game.peakroad.template.PeakRoadTemplate;
import com.xcity.game.skill.Skill;

// JSON: { id: 0, type: 0, hp: 0 }
public class PeakRoadCombatable implements CombatableBase {

	@JSONField(name = "0")
	protected long id;
	@JSONField(name = "1")
	protected byte type; // 0-player, 1-employee, 2-npc
	@JSONField(name = "2")
	protected int hp; // 剩余血量

	protected CombatableBase refer; // 实际引用的对象(player/employee/npc)
	
	// JSON parser invoke
	public PeakRoadCombatable() {}
	
	public PeakRoadCombatable(Player player) {
		this(player, PLAYER);
	}
	
	public PeakRoadCombatable(Employee employee) {
		this(employee, EMPLOYEE);
	}
	
	public PeakRoadCombatable(NpcCombatable npc) {
		this(npc, NPC);
	}
	
	private PeakRoadCombatable(CombatableBase refer, byte type) {
		this.id = refer.getId();
		this.refer = refer;
		this.type = type;
		this.hp = refer.getMaxHp(); // only init hp
	}

//	@JSONField(serialize = false)
//	@Override
//	public float getSTR() {
//		return refer.getSTR();
//	}
//
//	@JSONField(serialize = false)
//	@Override
//	public float getINT() {
//		return refer.getINT();
//	}
//
//	@JSONField(serialize = false)
//	@Override
//	public float getAGI() {
//		return refer.getAGI();
//	}
//
//	@JSONField(serialize = false)
//	@Override
//	public float getSPI() {
//		return refer.getSPI();
//	}
//
//	@JSONField(serialize = false)
//	@Override
//	public float getSTA() {
//		return refer.getSTA();
//	}
	
	@Override
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	@JSONField(serialize = false)
	@Override
	public byte getSex() {
		return refer.getSex();
	}

	@JSONField(serialize = false)
	@Override
	public String getName() {
		return refer.getName();
	}

	@JSONField(serialize = false)
	@Override
	public String getIcon() {
		return refer.getIcon();
	}

	@JSONField(serialize = false)
	@Override
	public int getLevel() {
		return refer.getLevel();
	}

	@Override
	public float getAttributeValue(CombatAttributeType type) {
		if (type == CombatAttributeType.HP) {
			return hp;
		}
		return refer.getAttributeValue(type);
	}

	@Override
	public float setAttributeValue(CombatAttributeType type, float value) {
		if (type == CombatAttributeType.HP) {
			hp = (int) value;
		}
		return value;
	}
	
	@Override
	public List<? extends Skill> getSkills() {
		return refer.getSkills();
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	@JSONField(serialize = true)
	@Override
	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public void init(PeakRoadInstance instance) {
		if (type == PLAYER) {
			this.refer = instance.getOwner();
		} else if (type == EMPLOYEE) {
			this.refer = instance.getOwner().getEmployees().get(getId());
		} else if (type == NPC) {
			PeakRoadTemplate template = PeakRoadUtils.getTemplate(instance.getFloor());
			if (template != null) {
				NpcTeamTemplate npcTeam = template.getNpcTeam(instance.getLastChallengeDiff());
				if (npcTeam != null) {
					NpcTemplate npc = npcTeam.find((int) getId());
					if (npc != null) {
						this.refer = (CombatableBase) npc.toCombatabale();
					}
				}
			}
		}
	}
	
	public CombatableBase refer() {
		return refer;
	}

}
